package ai.leinao.common.queue.impl;

import ai.leinao.common.domain.GatherData;
import ai.leinao.common.queue.MessageQueue;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.util.concurrent.LinkedBlockingQueue;

/**
 * 基于内存的数据队列
 *
 * @author zhong.wang
 * @date 2025/9/8
 **/
@Primary
@Component
public class InMemoryMessageQueue implements MessageQueue, DisposableBean {
    /**
     * 采集数据队列
     */
    private final LinkedBlockingQueue<GatherData> gatherDataToQueue;

    /**
     * 构造函数
     */
    public InMemoryMessageQueue() {
        gatherDataToQueue = new LinkedBlockingQueue<>();
    }

    /**
     * 添加采集数据
     * @param gatherData
     */
    @Override
    public void pushGatherData(GatherData gatherData) {
        gatherDataToQueue.offer(gatherData);
    }

    /**
     * 获取待计算数据
     * @return
     * @throws InterruptedException
     */
    @Override
    public GatherData pollGatherDataToCalculate() throws InterruptedException {
        return gatherDataToQueue.take();
    }

    /**
     * 销毁
     * @throws Exception
     */
    @Override
    public void destroy() throws Exception {
        gatherDataToQueue.clear();
    }
}
